package com.fhz.shop.ui.details;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.fhz.shop.callback.ApiResponse;
import com.fhz.shop.databinding.ActivityDetailsBinding;
import com.fhz.shop.entity.GoodsInfoLocal;
import com.fhz.shop.entity.HUser;
import com.fhz.shop.entity.UserCart;
import com.fhz.shop.util.ImageLoader;
import com.google.android.material.snackbar.Snackbar;
import com.tencent.mmkv.MMKV;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.Toast;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import java.util.Date;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class DetailsActivity extends AppCompatActivity {

    private ActivityDetailsBinding binding;

    DetailsViewModel mDetailsViewModel;
    int id;

    GoodsInfoLocal data;
    UserCart userCart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mDetailsViewModel = new ViewModelProvider(this).get(DetailsViewModel.class);

        Intent intent = getIntent();
        if(intent != null){
            id = intent.getIntExtra("id", 1);
        }
        mDetailsViewModel.getGoodsDetails(id).observe(this, goodsInfoLocalApiResponse -> {

            data = goodsInfoLocalApiResponse.getData();
            ImageLoader.load(this,data.getGoodsDefaultIcon(),binding.detailsBannerImage);
            binding.detailsTitle.setText(data.getGoodsDesc());
            binding.detailsPrice.setText(data.getGoodsDefaultPrice());
            ImageLoader.load(this,data.getGoodsDetailOne(),binding.detailsImageOne);
            ImageLoader.load(this,data.getGoodsDetailTwo(),binding.detailsImageTwo);

        });

        //加入购物车
        binding.detailsAddCart.setOnClickListener(v ->  {

            if(data != null){
                userCart = new UserCart();
                MMKV mmkv = MMKV.defaultMMKV();
                HUser userId = mmkv.decodeParcelable("user", HUser.class);
                userCart.setUserId(userId.getUserId());
//                userCart.setCarCreateTime(new Date().toString());
                userCart.setCarImage(data.getGoodsDefaultIcon());
                userCart.setCarNum(1);
                userCart.setCarShopName(data.getGoodsDesc());
                userCart.setCarPrice(Float.parseFloat(data.getGoodsDefaultPrice()));
                userCart.setCarTitle(data.getGoodsDesc());

                mDetailsViewModel.addCart(userCart).observe(this, userCartApiResponse -> {
                    if(userCartApiResponse.code == 0){
                        UserCart data1 = userCartApiResponse.getData();
                        System.out.println(data1);
                        Toast.makeText(this, "添加成功!", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(this, userCartApiResponse.msg, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });


        binding.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "添加购物车成功!", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

    }

    public static void launch(Context context,int id){
        Intent intent = new Intent(context, DetailsActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);
    }
}